//
//  AppDelegate.m
//  Light
//
//  Created by 骁唐 on 15/8/19.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "AppDelegate.h"
#import "SliderViewController.h"
#import "UserViewController.h"
#import "ViewController.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "UMessage.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "IQKeyboardManager.h"
#import "UMSocialSinaHandler.h"
#import "UserConfig.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize writeCharacteristic;
@synthesize peripheral;
@synthesize mgr;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.isUpdateHardWare = NO;//初始化 更新标记变量。
    self.updating = NO;//初始化  标记是否正在更新固件
    self.isCheckedUpadating = YES;//初始化 重新打开应用检测是否处于升级状态
    self.isWirteOK = NO;//初始化 写入标记NO
    self.markIsgetHardNumber = NO;//初始化用户 不是注册进入的。NO
    self.markGo = @"00";//标记绑定界面 是注册进来的 默认是@"00"  
    self.isHeading = NO;
    self.isHearting = NO;

    IQKeyboardManager * manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;//控制整个功能是否启用。
    manager.shouldResignOnTouchOutside = YES; //控制点击背景是否收起键盘。
    manager.shouldToolbarUsesTextFieldTintColor = YES; //控制键盘上的工具条文字颜色是否用户自定义。
    manager.enableAutoToolbar = NO;//控制是否显示键盘上的工具条。
    
    //设置状态栏为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    //友盟分享
    [UMSocialData setAppKey:UMKEY];
    //设置微信Key
    [UMSocialWechatHandler setWXAppId:@"wx1353baf4f620a11d" appSecret:@"b7a7afc81e9fc5beaed049d08621bdd5" url:@"http://www.umeng.com/social"];
    //设置qqKey
    [UMSocialQQHandler setQQWithAppId:@"1104876762" appKey:@"0LAwbSmmUZ0ZmmNS" url:@"http://www.umeng.com/social"];
    //设置新浪微博
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    // 友盟推送
    [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
     |UIRemoteNotificationTypeSound
     |UIRemoteNotificationTypeAlert];
    [UMessage startWithAppkey:@"559cc67867e58ec4f20012a0" launchOptions:launchOptions];
    //register remoteNotification types
    
    if (TJJiOS8) {
        //register remoteNotification types （iOS 8.0及其以上版本）
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=@"Accept";
        action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2_identifier";
        action2.title=@"Reject";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序，在后台处理
        action2.authenticationRequired = YES;//需要解锁才能处理，如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略；
        action2.destructive = YES;
        
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"category1";//这组动作的唯一标示 //SXW__iOS8以下崩溃标记。
        [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
        
        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
                                                                                     categories:[NSSet setWithObject:categorys]];
        [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
    }
    else
    {//register remoteNotification types (iOS 8.0以下)
        [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
         |UIRemoteNotificationTypeSound
         |UIRemoteNotificationTypeAlert];
    }
    [UMessage setLogEnabled:YES];
    
    
    //设置蒲公英统计SDK
    [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:PGY_APPKEY];//启动基本SDK
    [[PgyManager sharedPgyManager] startManagerWithAppId:PGY_APPKEY];//启动更新检查SDK
    [[PgyManager sharedPgyManager] setEnableFeedback:NO];//关闭用户反馈功能(默认开启)
    return YES;
}



#pragma mark -
#pragma mark 分享使用
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url] || [TencentOAuth HandleOpenURL:url];
}
#pragma mark -
#pragma mark 打开连接
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url]|| [TencentOAuth HandleOpenURL:url];
}
#pragma mark -
#pragma mark 注册
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [UMessage registerDeviceToken:deviceToken];
    NSLog(@"%@",deviceToken);//token值
    
    NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                  stringByReplacingOccurrencesOfString: @">" withString: @""]
                 stringByReplacingOccurrencesOfString: @" " withString: @""]);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UMessage didReceiveRemoteNotification:userInfo];
}

#pragma mark -
#pragma mark 应用将变为不活跃状态
- (void)applicationWillResignActive:(UIApplication *)application {
    //应用将变为不活跃状态，发送通知，获取当前时间
    NSNotification *notification =[NSNotification notificationWithName:@"resignActive" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

#pragma mark -
#pragma mark 应用进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
#pragma mark -
#pragma mark 应用从后台苏醒
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //应用苏醒，发送通知，获取当前时间
    NSNotification *notification =[NSNotification notificationWithName:@"becomeActive" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

#pragma mark -
#pragma mark 应用中断
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
//    UserConfig *config = [UserConfig sharingUserConfig];
    if (self.sleepModel == YES) {
//    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//        if (delegate.startSleepModelTime>0) {
//            NSString *currentTime = [[NSString alloc]initWithFormat:@"%lld",delegate.startSleepModelTime];
//            [config setStopwatchTime:currentTime];
//        }
    }
}

@end
